import jieba
import os

ls={}
ld={}
def tianjia():
    x=input("请输入释义（可以多重释义）：")
    y=input("请输入单词：")
    
    fo=open("EnglishDictionary.txt","r")
    for i in fo.readlines():     
        s=i.split()
        if s[0]==y:
            print("该单词已添加到字典库")
            break
    fo.close()
    
    fo=open("EnglishDictionary.txt","a")
    
    ls[y]=x
    
    ld[y]=x
    
    fo.write(" ".join(ld)+" ")
    fo.writelines(ld.get(y)+"\n")
    ld.clear()
    
    fo.close()
    
    print("添加成功!!!")

    
def chaxun():
    c=input("请输入要查找英文：")
    fo=open("EnglishDictionary.txt","r")
    flag=0
    for i in fo.readlines():     
        s=i.split()
        if s[0]==c:
            print(s)
            flag=1
            print("查找成功!!!")
        
    fo.close()
    
def duoyi():
    print("tip:在词库已有的单词添加多重释义")
    g=input("请输入要增加释义的单词：")
    flag=0
    lf={}
    fo=open("EnglishDictionary.txt","r+")
    for i in fo.readlines():     
        b=i.split()
        
        p=b[0]
        nn=b[1]
        if b[0]==g:
            
            flag=1
            yuan=b[1]
            print("查找成功!!!可以添加多重释义！")
            shiyi=input("请直接输入要添加的释义")
            lf[p]=shiyi
            fo.write(" ".join(lf)+" ")
            fo.writelines(nn+","+shiyi+"\n")
            fo.close()
            
            lines = (k for k in open('EnglishDictionary.txt', 'r') if i not in k )
            f = open('cidian_new.txt', 'w')
            f.writelines(lines)
            f.close()
            os.rename('EnglishDictionary.txt', 'EnglishDictionary.bak')
            os.rename('EnglishDictionary_new.txt', 'EnglishDictionary.txt')
            os.remove('EnglishDictionary.bak')


            print("释义添加成功！")
            break
        
    
    
def main():
    a=1
    while(a<10):
        print("tip：数字1（添加），数字2（查找）,数字3（退出），数字4（添加多义）")
        f=eval(input("请输入要执行的操作:"))
        if f==1:
            tianjia()
        elif f==2:
            chaxun()
        elif f==3:
            break
        elif f==4:
            duoyi()
        else:
            print("输入有误！")
            break
main()
